function HandleDialogLocation(gui, item, options) {
  this._gui = gui;
  this._item = item;
  this._options = options;
  this._popupBox = new PopupBox();
  
  this._ceremony = (this._item.ceremony == '');
  this._reception = (this._item.reception == '');
  
  this._createElements();
};

HandleDialogLocation.prototype._createElements = function() {
  this._loadData();
};

HandleDialogLocation.prototype._loadData = function() {
  DlmanageUtils.clear(this._gui);
  var img = DOMUtils.getLoadingImage();
  this._gui.content.appendChild(img);
  
  this._receptions = null;
  this._ceremonys = null;
  
  this._retrieveReceptions();
  this._retrieveCeremonys();
};

HandleDialogLocation.prototype._verifyData = function() {
  if (this._receptions && this._ceremonys) {
    this._updateElements();
  }
};


HandleDialogLocation.prototype._retrieveReceptions = function() {
  var _self = this;
  /*new RequestUtils()._read('ireception', null, null, null, null, 'd.name', null, function(result, params) { _self._receptions = result.data;
                                                                                                        _self._verifyData.call(_self);
                                                                                                      }, null);
*/

  new RequestUtils()._mysql('ireception', null, function(result, params) { _self._receptions = result.data;
                                                                          _self._verifyData.call(_self);
                                                                        }, {php:'customphp/mysqlread.php'});  
};

HandleDialogLocation.prototype._retrieveCeremonys = function() {
  var _self = this;
  /*new RequestUtils()._read('iceremony', null, null, null, null, 'd.name', null, function(result, params) { _self._ceremonys = result.data;
                                                                                                       _self._verifyData.call(_self);
                                                                                                     }, null);
*/
  new RequestUtils()._mysql('iceremony', null, function(result, params) { _self._ceremonys = result.data;
                                                                          _self._verifyData.call(_self);
                                                                        }, {php:'customphp/mysqlread.php'});  

};

HandleDialogLocation.prototype._updateElements = function() {
  var select, img, table, tr, td, _self = this;
  DlmanageUtils.clear(this._gui);
  
  table = document.createElement('table');
  this._gui.content.appendChild(table);
  
  if (this._ceremony) {
    tr = table.insertRow(-1);
    td = tr.insertCell(-1);
    td.appendChild(document.createTextNode('Ceremony'));
    td = tr.insertCell(-1);
    select = document.createElement('select');
    td.appendChild(select);
    select.options[select.options.length] = new Option('');
    select.style.width = '200px';
    for (var i = 0, il = this._ceremonys.length; i < il; i++) {
      var ceremony = this._ceremonys[i];
      var option = new Option(ceremony.name);
      select.options[select.options.length] = option;
      if (option.text == this._item.ceremony){
        select.selectedIndex = select.options.length - 1;
      }
    }
    select.onchange = function() { _self._item.ceremony = this.options[this.selectedIndex].text; };
    
    td = tr.insertCell(-1);
    var add = DOMUtils.createInput('', '', 'button', '', null);
    add.className = "add";
    add.onclick = function() { var object = ICeremony.instance();
                               var div = document.createElement('div');
                               var pos = DOMUtils.findPos(this);
                               var func1 = function() { _self._popupBox._close();
                                                        _self._item.ceremony = object.name;
                                                        new RequestUtils()._mwrite('iceremony', [object], [], function() { _self._retrieveCeremonys.call(_self); }, { pos: pos });
                                                      };
                               var func2 = function() { _self._popupBox._close(); };
                               MiscUtils.dialog(_self._popupBox, pos, div, func1, func2, { ok: 'Add' });
                               new DialogIObject(div, object, { name: 'iceremony', title: 'Ceremony Locations',php:'customphp/mysqlread.php'});
                             };
    td.appendChild(add);
  }
  
  if (this._reception) {
    tr = table.insertRow(-1);
    td = tr.insertCell(-1);
    td.appendChild(document.createTextNode('Reception'));
    td = tr.insertCell(-1);
    select = document.createElement('select');
    td.appendChild(select);
    select.options[select.options.length] = new Option('');
    select.style.width = '200px';
    for (var i = 0, il = this._receptions.length; i < il; i++) {
      var ceremony = this._receptions[i];
      var option = new Option(ceremony.name);
      select.options[select.options.length] = option;
      if (option.text == this._item.reception){
        select.selectedIndex = select.options.length - 1;
      }
    }
    select.onchange = function() { _self._item.reception = this.options[this.selectedIndex].text; };
    
    td = tr.insertCell(-1);
    var add = DOMUtils.createInput('', '', 'button', '', null);
    add.className = "add";
    add.onclick = function() { var object = ICeremony.instance();
                               var div = document.createElement('div');
                               var pos = DOMUtils.findPos(this);
                               var func1 = function() { _self._popupBox._close();
                                                        _self._item.reception = object.name;
                                                        new RequestUtils()._mwrite('ireception', [object], [], function() { _self._retrieveReceptions.call(_self); }, { pos: pos });
                                                      };
                               var func2 = function() { _self._popupBox._close(); };
                               MiscUtils.dialog(_self._popupBox, pos, div, func1, func2, { ok: 'Add' });
                               new DialogIObject(div, object, { name: 'ireception', title: 'Reception Locations',php:'customphp/mysqlread.php'});
                             };
    td.appendChild(add);
  }
};

